




//
//  BasePayVC.m
//  GoldBook
//
//  Created by wmz on 2021/8/26.
//  Copyright © 2021 wmz. All rights reserved.
//

#import "BasePayVC.h"
#import "AppDelegate.h"
@interface BasePayVC ()<WXApiDelegate>

@end

@implementation BasePayVC
- (void)hd_setupViews{
    [super hd_setupViews];
    [QDOrderNetWork.shareInstance requestPayPower:@{@"mId":GNFillEmpty(PostManage.shareInstance.userModel.ids)} viewController:self andBlock:^(id  _Nullable data, BOOL result) {
        
        if (result) {
            NSArray *marr = [NSArray yy_modelArrayWithClass:QDPayMethodModel.class json:data];
            [self getPayMethod:marr];
        }
    }];
}

- (void)hd_getWillData{
    
}

- (void)getPayMethod:(NSArray<QDPayMethodModel*>*)methods{
    
}

- (void)showPaySheet:(PostResultID)block{
    Dialog().wTitleSet(@"请选择支付方式")
    .wSelectShowCheckedSet(YES)
    .wAnimationDurtionSet(0.25)
    .wShowAnimationSet(AninatonShowTop)
    .wHideAnimationSet(AninatonHideTop)
    .wTextAlignmentSet(NSTextAlignmentLeft)
    .wCellHeightSet(PostNum(44))
    .wOKColorSet(wColor_yellow)
    .wSeparatorStyleSet(UITableViewCellSeparatorStyleSingleLine)
    .wTypeSet(DialogTypeSheet).wDataSet(@[@{@"name":@"微信支付",@"image":@"mine_22"},
                                          @{@"name":@"支付宝支付",@"image":@"mine_23"}])
    .wEventFinishSet(^(id anyID, NSIndexPath *path, DialogType type) {
        if (block) {
            block(anyID);
        }
    }).wStartView(DialogWindow);
}

/// 支付宝支付
- (void)ApiPayAction:(NSString*)payOrder{
    if (![payOrder isKindOfClass:NSString.class]) return;
    @HDWeakify(self)
     AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
     delegate.ZFBCallBack = ^(id dataPay) {
         @HDStrongify(self)
        if (dataPay) [self checkZFBOrder:dataPay];
     };
    [[AlipaySDK defaultService] payOrder:payOrder fromScheme:@"alisdkzuanba" callback:^(NSDictionary *resultDic) {
        @HDStrongify(self)
        if (resultDic) [self checkZFBOrder:resultDic];
    }];
}

/// 微信支付
- (void)WXPayAction:(NSDictionary*)info{
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    delegate.wxDelegate = self;
    [WXApi sendReq:[WXUntils wxPayDic:info] completion:^(BOOL success) {
        if (!success) PostAutoAlert(@"调起微信支付失败,请稍后再试")
    }];
}

/// 微信回调
-(void)onResp:(BaseResp *)resp {
    if (resp.errCode != WXErrCodeUserCancel) {
        if (resp.errCode == WXSuccess) {
            [self payResult:YES error:nil];
        }else{
            [self payResult:NO error:@"支付失败"];
        }
    }
}
/// 检测支付宝支付结果
- (void)checkZFBOrder:(NSDictionary*)resultDic{
   NSInteger orderState=[resultDic[@"resultStatus"]integerValue];
   if (orderState==9000) {
       [self payResult:YES error:nil];
    }else{
        NSString *returnStr;
        switch (orderState) {
        case 8000:
          returnStr=@"订单正在处理中";
          break;
        case 4000:
          returnStr=@"订单支付失败";
          break;
        case 6001:
          returnStr=@"订单取消";
          break;
        case 6002:
          returnStr=@"网络连接出错";
          break;
        default:
          break;
         }
        [self payResult:NO error:returnStr];
    }
}

/// 支付结果
- (void)payResult:(BOOL)success error:(nullable NSString*)errStr{
    if (success) {
        PostSuccessView(@"支付成功",DialogWindow)
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popToRootViewControllerAnimated:YES];
        });
    }else{
        PostError(errStr?:@"支付失败")
    }
}

@end
